/**
 * Created by Administrator on 2017/7/20 0020.
 */
var gulp = require("gulp"),
    livereload = require("gulp-livereload"),
    webserver = require("gulp-webserver"),
    sass = require("gulp-ruby-sass"),
    uglify = require("gulp-uglify"),
    //jshint = require("gulp-jshint"),
    rename = require("gulp-rename"),
    imagemin = require("gulp-imagemin"),
    pngquant = require("imagemin-pngquant");

// 自动刷新网站
gulp.task("webserver",function(){
    gulp.src("dest")
        .pipe(webserver({
            livereload:true,
            open:true
        }))
});

//html文件拷到发布目录
gulp.task("html",function(){
    return gulp.src("src/**/*.html")
        .pipe(gulp.dest("dest/"));
});

//sass转译并拷到发布目录
gulp.task("sass",function(){
    return sass("src/scss/*.scss",{style:"compact"})
        .on("error",function(err){
            console.log(err)
        })
        .pipe(rename({suffix:".min"}))
        .pipe(gulp.dest("dest/css/"));
});

//js检查格式
//gulp.task("js",function(){
//    return gulp.src("src/js/**/*.js")
//        .pipe(jshint())
//        .pipe(jshint.reporter("default"));
//});

//js压缩并拷到发布目录
gulp.task("script",function(){
    return gulp.src("src/js/**/*.js")
        .pipe(uglify())
        .pipe(rename({suffix:".min"}))
        .pipe(gulp.dest("dest/js/"))
});

//图片压缩并拷到发布目录
gulp.task("img",function(){
    return gulp.src("src/img/*")
        .pipe(imagemin({
            progressive:true,
            svgoPlugins:[{removeViewBox:false}],
            use:[pngquant()]
        }))
        .pipe(gulp.dest("dest/img/"))
});

//服务器和json文件拷到发布目录
gulp.task("server",function(){
    return gulp.src("src/server/*")
        .pipe(gulp.dest("dest/server/"))
});

//监听任务
gulp.task("watch",function(){
    gulp.watch("src/**/*.html",["html"]);
    gulp.watch("src/scss/**/*.scss",["sass"]);
    //gulp.watch("src/js/**/*.js",["js"]);
    gulp.watch("src/js/**/*.js",["script"]);
    gulp.watch("src/img/*",["img"]);
    gulp.watch("src/server/*",["server"]);
});

gulp.task("default",["webserver","html","sass","script","img","server","watch"]);